package com.hengpeng.msls.port;

import java.net.URLDecoder;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;

import com.hengpeng.itfinbase.constants.RespCodeConstants;
import com.hengpeng.itfinbase.constants.TransCodeConstants;
import com.hengpeng.itfinbase.message.partner.NotifyRequestMsg;
import com.hengpeng.itfinbase.message.partner.NotifyResponseMsg;
import com.hengpeng.msls.controller.BaseController;
import com.hengpeng.msls.tools.JsonUtils;

/**
 * <pre>
 * APP端通知回调处理
 * </pre>
 * 
 * @author weiyuan
 * @version 1.0, 2015-8-17
 */
@Controller
@RequestMapping(value = "/appNotify")
public class AppController extends BaseController {
    private Log logger = LogFactory.getLog(AppController.class);

    /**
     * <pre>
     * APP端页面返回
     * </pre>
     * 
     * @param map
     * @param request
     * @param response
     * @param providerId
     * @return
     * @throws Exception
     */
    @SuppressWarnings("unchecked")
    @RequestMapping(value = "/pgNotify")
    public String pgNotify(ModelMap map, HttpServletRequest request, HttpServletResponse response) throws Exception {
        NotifyRequestMsg requestMessage = new NotifyRequestMsg();
        requestMessage.setIsBgNotify(false);
        requestMessage.setTransType(TransCodeConstants.PartnerTransCode.NOTIFY);
        StringBuffer parameterStr = new StringBuffer();
        Map<String, String> parameterMap = new HashMap<String, String>();
        Enumeration<String> params = request.getParameterNames();
        while (params.hasMoreElements()) {
            String paramName = params.nextElement();
            parameterStr.append("&" + paramName + "=" + URLDecoder.decode(request.getParameter(paramName), "utf-8"));
            parameterMap.put(paramName, URLDecoder.decode(request.getParameter(paramName), "utf-8"));
        }
        if (parameterStr.length() > 0) {
            parameterStr.delete(0, 1);
        }
        requestMessage.setParamString(JsonUtils.createGson().create().toJson(parameterMap));
        requestMessage.setParameter(parameterMap);
        logger.info("第三方页面返回参数{" + parameterStr.toString() + "}");
        NotifyResponseMsg port_result = (NotifyResponseMsg) serviceFactory.getProtFactory().remoteTrans(requestMessage);
        refreshSessionMember(request);
        map.put("resultObj", port_result);
        return "/page/common_page_result.jsp";
    }

    /**
     * <pre>
     * APP端服务端响应
     * </pre>
     * 
     * @param map
     * @param request
     * @param response
     * @return
     * @throws Exception
     */
    @SuppressWarnings("unchecked")
    @RequestMapping(value = "/bgNotify")
    public void bgNotify(ModelMap map, HttpServletRequest request, HttpServletResponse response) throws Exception {
        NotifyRequestMsg requestMessage = new NotifyRequestMsg();
        requestMessage.setIsBgNotify(true);
        requestMessage.setTransType(TransCodeConstants.PartnerTransCode.NOTIFY);
        Map<String, String> parameter = new HashMap<String, String>();
        Enumeration<String> params = request.getParameterNames();
        while (params.hasMoreElements()) {
            String paramName = params.nextElement();
            parameter.put(paramName, request.getParameter(paramName));
        }
        requestMessage.setParamString(JsonUtils.createGson().create().toJson(parameter));
        requestMessage.setParameter(parameter);
        logger.info("第三方后台通知参数{" + parameter.toString() + "}");
        NotifyResponseMsg port_result = (NotifyResponseMsg) serviceFactory.getProtFactory().remoteTrans(requestMessage);
        refreshSessionMember(request);
        map.put("param", port_result.getReturnNotifyParamValue());
        if (port_result.getRespCode().equalsIgnoreCase(RespCodeConstants.CommonCode.TRANS_SUC)) {
            response.getWriter().print(port_result.getReturnNotifyParamValue());
        }
    }
}